home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / Library / Sound / SoundItem.as next >
Encoding:
Text File  |  2007-09-27  |  5.4 KB  |  209 lines

  1. class Library.Sound.SoundItem
  2. {
  3.    var sndObj;
  4.    var sLinkage;
  5.    var mcRef;
  6.    var nRemainingLoop;
  7.    var nCurrentVolume;
  8.    var nTargetVolume;
  9.    var nFadeRate;
  10.    var sCategory;
  11.    var bFadeAtEnd;
  12.    var bMuted;
  13.    var bPaused;
  14.    var bNeedFreshStart;
  15.    var nCurrentTime;
  16.    var aEventListeners;
  17.    var bStopAfterFade;
  18.    static var FADE_RATE = 8;
  19.    static var FADE_AT_END_TIME = 800;
  20.    function SoundItem(__sndObject, __sLinkage, __nVolume, __nLoop, __sCategory, __mc)
  21.    {
  22.       this.sndObj = __sndObject;
  23.       this.sLinkage = __sLinkage;
  24.       this.mcRef = __mc;
  25.       this.nRemainingLoop = __nLoop;
  26.       this.nCurrentVolume = __nVolume;
  27.       this.nTargetVolume = __nVolume;
  28.       this.nFadeRate = Library.Sound.SoundItem.FADE_RATE;
  29.       this.sCategory = __sCategory;
  30.       this.bFadeAtEnd = false;
  31.       this.bMuted = false;
  32.       this.bPaused = false;
  33.       this.bNeedFreshStart = false;
  34.       this.sndObj.onSoundComplete = Library.Utils.Delegate.create(this,this.doSoundComplete);
  35.       this.sndObj.start(0,__nLoop);
  36.       this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  37.       this.nCurrentTime = 0;
  38.       this.aEventListeners = new Array();
  39.    }
  40.    function doEnterFrame()
  41.    {
  42.       if(!this.bPaused)
  43.       {
  44.          if(this.nCurrentTime > this.sndObj.position)
  45.          {
  46.             this.nRemainingLoop = this.nRemainingLoop - 1;
  47.          }
  48.          this.nCurrentTime = this.sndObj.position;
  49.       }
  50.       this.doCheckFadeAtEnd();
  51.       this.doManageFade();
  52.    }
  53.    function doAddListener(__oListener)
  54.    {
  55.       this.aEventListeners.push(__oListener);
  56.    }
  57.    function doRemoveListener(__oListener)
  58.    {
  59.       var _loc2_ = 0;
  60.       while(_loc2_ < this.aEventListeners.length)
  61.       {
  62.          if(this.aEventListeners[_loc2_] == __oListener)
  63.          {
  64.             delete this.aEventListeners[_loc2_];
  65.             this.aEventListeners.splice(_loc2_,1);
  66.          }
  67.          _loc2_ = _loc2_ + 1;
  68.       }
  69.    }
  70.    function doSoundComplete()
  71.    {
  72.       if(this.bNeedFreshStart && this.nRemainingLoop > 1)
  73.       {
  74.          this.sndObj.start(0,this.nRemainingLoop - 1);
  75.       }
  76.       else
  77.       {
  78.          this.doManageEndEvent();
  79.       }
  80.    }
  81.    function doUpdateSound()
  82.    {
  83.       this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  84.    }
  85.    function doMute()
  86.    {
  87.       this.bMuted = true;
  88.       this.doUpdateSound();
  89.    }
  90.    function doUnMute()
  91.    {
  92.       this.bMuted = false;
  93.       this.doUpdateSound();
  94.    }
  95.    function doPause()
  96.    {
  97.       this.sndObj.stop();
  98.       this.bPaused = true;
  99.    }
  100.    function doResume()
  101.    {
  102.       this.bPaused = false;
  103.       this.bNeedFreshStart = true;
  104.       this.sndObj.start(this.nCurrentTime / 1000,1);
  105.    }
  106.    function doStop()
  107.    {
  108.       this.sndObj.stop();
  109.       this.doManageEndEvent();
  110.    }
  111.    function doFadeTo(__nVolume, __bStopAndDelete)
  112.    {
  113.       if(__bStopAndDelete == undefined)
  114.       {
  115.          __bStopAndDelete = true;
  116.       }
  117.       this.bStopAfterFade = __bStopAndDelete;
  118.       this.nTargetVolume = __nVolume;
  119.    }
  120.    function setFadeRate(__nRate)
  121.    {
  122.       if(__nRate == undefined)
  123.       {
  124.          __nRate = Library.Sound.SoundItem.FADE_RATE;
  125.       }
  126.       this.nFadeRate = __nRate;
  127.    }
  128.    function setFadeAtEnd(__bFadeAtEnd)
  129.    {
  130.       this.bFadeAtEnd = true;
  131.    }
  132.    function setPan(__nPan)
  133.    {
  134.       this.sndObj.setPan(__nPan);
  135.    }
  136.    function get Category()
  137.    {
  138.       return this.sCategory;
  139.    }
  140.    function get LinkageName()
  141.    {
  142.       return this.sLinkage;
  143.    }
  144.    function doDestroy()
  145.    {
  146.       this.sndObj.stop();
  147.       delete this.sndObj;
  148.       this.mcRef.removeMovieClip();
  149.    }
  150.    function doCheckFadeAtEnd()
  151.    {
  152.       if(this.bFadeAtEnd)
  153.       {
  154.          if(this.nRemainingLoop == 1)
  155.          {
  156.             if(this.sndObj.duration - this.nCurrentTime <= Library.Sound.SoundItem.FADE_AT_END_TIME)
  157.             {
  158.                this.doFadeTo(0);
  159.             }
  160.          }
  161.       }
  162.    }
  163.    function doManageEndEvent()
  164.    {
  165.       var _loc2_ = 0;
  166.       while(_loc2_ < this.aEventListeners.length)
  167.       {
  168.          this.aEventListeners[_loc2_].doSoundEvent(Library.Sound.SoundManager.EVENT_SOUND_COMPLETE,this);
  169.          _loc2_ = _loc2_ + 1;
  170.       }
  171.       this.aEventListeners = new Array();
  172.       this.mcRef.removeMovieClip();
  173.       delete this.mcRef;
  174.       delete this.aEventListeners;
  175.       delete this.sndObj.onSoundComplete;
  176.       delete this.sndObj;
  177.    }
  178.    function doManageFade()
  179.    {
  180.       if(this.nCurrentVolume != this.nTargetVolume)
  181.       {
  182.          this.nCurrentVolume = Library.Utils.MoreMath.getReachNum(this.nCurrentVolume,this.nTargetVolume,this.nFadeRate);
  183.          this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  184.       }
  185.       if(this.nCurrentVolume <= 0 && this.bStopAfterFade)
  186.       {
  187.          this.sndObj.stop();
  188.          this.doManageEndEvent();
  189.       }
  190.    }
  191.    function returnComputedVolume(__nVolume)
  192.    {
  193.       var _loc2_ = undefined;
  194.       if(!this.bMuted && !Library.Sound.SoundManager.isCategoryMuted(this.sCategory))
  195.       {
  196.          var _loc3_ = Library.Sound.SoundManager.MasterVolume / 100;
  197.          var _loc4_ = Library.Sound.SoundManager.getCategoryVolume(this.sCategory) / 100;
  198.          _loc2_ = __nVolume;
  199.          _loc2_ *= _loc4_;
  200.          _loc2_ *= _loc3_;
  201.       }
  202.       else
  203.       {
  204.          _loc2_ = 0;
  205.       }
  206.       return _loc2_;
  207.    }
  208. }
  209.